package cn.xxxy.fourm.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xxxy.fourm.dao.BoardDao;
import cn.xxxy.fourm.entity.Board;
import cn.xxxy.fourm.entity.BoardState;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.entity.UserRole;
import cn.xxxy.fourm.entity.UserState;
import cn.xxxy.fourm.model.BoardDto;
import cn.xxxy.fourm.model.BoardStateDto;
import cn.xxxy.fourm.model.DataGrid;
import cn.xxxy.fourm.model.Message;
import cn.xxxy.fourm.model.UserStateDto;
import cn.xxxy.fourm.service.BoardService;
import cn.xxxy.fourm.service.BoardStateService;
@Controller
public class BoardController {
	@Autowired
	private BoardService boardService;
	@Autowired
	private BoardStateService boardStateService;
	@Autowired
	private BoardDao boardDao;
	/**
	 * 1.显示
	 * @return
	 */
	@RequestMapping("/listBoards")
	public String listBoards() {
		return "/board/boards";
	}
	/**
     * 2、显示列表数据
     * @param page
     * @param rows
     * @return
     */ 
	@RequestMapping("/list")
	@ResponseBody
	public DataGrid<BoardDto> list1(int page,int rows) {
		List<BoardDto> lists=boardService.listPage(page,rows);
		Long count = boardDao.count(); 
		DataGrid<BoardDto> datagrid=new DataGrid<BoardDto>();
		datagrid.setRows(lists);
		datagrid.setTotal(count);
		datagrid.setSuccess(true);
		return datagrid;
	}
	/**
     * 3.添加操作
     * @param board
     * @return
     */
	@ResponseBody 
	@RequestMapping("/board/save") 
	public Message save(Board board) {
		Message message=new Message();
		BoardState boardState=new BoardState();
		boardState.setId(1);
		board.setBoardState(boardState);
		Board board1=boardDao.saveAndFlush(board);
		if(board1!=null) {
			message.setMsg("恭喜您，添加成功");
			message.setSuccess(true);
		}else {
		message.setMsg("很遗憾，添加失败");
		message.setSuccess(false);
		}
		return message; 
		}
	/**
     * 4删除操作
     * @param ids
     * @return
     */ 
	@ResponseBody 
    @RequestMapping("/board/deletes") 
	public Message delete(@RequestParam(value = "ids") String ids) {
		Message message=new Message();
		String[] idsStr = ids.split(","); 
		for (int i = 0; i < idsStr.length; i++) { 
			boardDao.deleteById(Integer.parseInt(idsStr[i]));
			} 
		message.setMsg("恭喜您，添加成功");
		message.setSuccess(true); 
		return message; 
		}
	/**
	 * 5.更新操作
	 */
	@RequestMapping("/board/update")
	@ResponseBody
	public Message updateBoard(Board board,int bid,int id) {
		Message message=new Message();
		BoardState boardState=new BoardState();
		boardState.setId(id);
		board.setBoardState(boardState);
		board.setBid(bid);
		Board board1=boardDao.saveAndFlush(board);
		if(board1!=null) {
			message.setMsg("恭喜您，更新成功");
			message.setSuccess(true);
		}else {
			message.setMsg("很遗憾，更新失败");
			message.setSuccess(false);
		}
		return message;
	}
	/**
	 * 6.查询版块状态
	 */
	@ResponseBody 
	@RequestMapping("/board/states") 
	public List<BoardStateDto> states(){
		List<BoardStateDto> lists=boardStateService.listBoardStates();
		return lists;		
	}
}
